<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/all.conf.php');
require_once(__PHY_LIB.'/inc/page.inc.php');
require_once(__PHY_LIB.'/conf/nation.conf.php');
require_once(__PHY_LIB.'/class/Hot.class.php');

$rReq = validate($_GET, 'nation:e[mala,singapore,thailand]|id:n|brand:u|goods:u|pg:n');
$oTpl->assign('rNation', $_NATION[$rReq['nation']]);

/// one detail ///
if (0 < $rReq['id']) {
	$oDetail = new Hot($rReq['id']);
	$oTpl->assign('rDetail', $oDetail->getData());
}

/// brand list ///
$oList = new HotList();
$oList->cfgField("DISTINCT brand");
$oList->cfgWhere("nation='".addslashes($rReq['nation'])."'");
$oList->load();
$oTpl->assign('rCitys', $oList->fetchArray());
if (0 < $rReq['id']) {
	$rReq['brand'] = $oDetail->get('brand');
}
elseif (!$rReq['brand'] && ($oOne = $oList->nextItem())) {
	$rReq['brand'] = $oOne->get('brand');
}

/// goods list ///
$oList = new HotList();
$oList->cfgField("DISTINCT goods");
$oList->cfgWhere("nation='".addslashes($rReq['nation'])."'".($rReq['brand'] ? " AND brand='".addslashes($rReq['brand'])."'" : ""));
$oList->load();
$oTpl->assign('rRestaurants', $oList->fetchArray());
if (0 < $rReq['id']) {
	$rReq['goods'] = $oDetail->get('goods');
}
elseif (!$rReq['goods'] && ($oOne = $oList->nextItem())) {
	$rReq['goods'] = $oOne->get('goods');
}

/// list ///
if (!(0 < $rReq['id'])) {
	$rPager = array(
		'Sze' => 10,
		'Req' => 'pg',
		'Num' => max(1, $rP['pg']),
		'Bse' => http_build_query(array_slice($rReq, 0, -1)),
	);
	$oList = new HotList();
	$oList->cfgWhere("nation='".addslashes($rReq['nation'])."'".($rReq['brand'] ? " AND brand='".addslashes($rReq['brand'])."'".($rReq['goods'] ? " AND goods='".addslashes($rReq['goods'])."'" : "") : ""));
	$rPager['Cnt'] = $oList->countAll();
	$rPager['Max'] = ceil($rPager['Cnt']/$rPager['Sze']);
	$oList->cfgOrder("id DESC");
	$oList->load();
	$oTpl->assign('rList', $oList->fetchArray());
	$oTpl->assign('rPager', $rPager);
}

/// html ///
$oTpl->assign('rReq', $rReq);
$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').".tpl");
?>
